package _6ddns;

import _0util.HttpUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Description:
 * @Author wangzhen
 * @Date 2022-05-06 21:56
 **/
public class TencentDDNS {

    public static final String GET_IP_URL="http://ip.03k.org";

    //DNSPod-Token的ID
    public static final String DNSPOD_TOKEN_ID="12345";

    //在DNSPod-Token的Token
    public static final String DNSPOD_TOKEN="esagdfgdfg";

    //要做修改的二级域名
    public static final String DOMAIN="abc.com";

    //主机记录名，例如www.baidu.com的主机记录名是www，home.abc.com的主机记录名是home，只想修改主域名可填写@
    public static final String HOME="@";

    //日志文件位置
    public static final String LOG_FILE_URL="./ddnsRun.log";

    public static void main(String[] args) throws IOException {

        FileWriter fileWriter=new FileWriter(LOG_FILE_URL,true);
        Date date=new Date();
        try {

            //1.查询域名信息
            Map<String,String> params1=new HashMap<>();
            params1.put("domain",DOMAIN);
            params1.put("record_type","A");
            params1.put("login_token",DNSPOD_TOKEN_ID+","+DNSPOD_TOKEN);//接口需要tokenID+token组合发送
            params1.put("format","json");
            String post = HttpUtil.post("https://dnsapi.cn/Record.List", null, params1, "utf-8");
            JSONObject res = JSONObject.parseObject(post);
            JSONObject statusRes = JSONObject.parseObject(res.getString("status"));

            //如果请求失败，打印失败信息
            if(!statusRes.getString("code").equals("1")){
                String msg=DateUtil.formatDateTime(date) +" error->"+statusRes.getString("message")+"\n";
                fileWriter.write(msg);
                return;
            }


            //因为查询的是二级域名，会返回该域名的所有子域名，需要找出要修改的那条记录
            JSONArray records = JSONArray.parseArray(res.getString("records"));
            JSONObject record=null;
            for(int i=0;i<records.size();i++){
                if(records.getJSONObject(i).getString("name").equals(HOME)){
                    record=records.getJSONObject(i);
                    break;
                }
            }

            if(record==null){
                String msg=DateUtil.formatDateTime(date) +" error->暂无"+HOME+"相关的域名记录\n";
                fileWriter.write(msg);
                return;
            }


            //获取当前最新IP
            String ip = HttpUtil.get(GET_IP_URL, null, null);



            //2.判断IP是否一致，若一致则不执行修改
            String oldIp=record.getString("value");
            if(oldIp.equals(ip)){
                String msg=DateUtil.formatDateTime(date) +" info->IP一致，无需修改\n";
                fileWriter.write(msg);
                return;
            }


            //3.不一致，执行修改
            Map<String,String> params2=new HashMap<>();
            params2.put("domain",DOMAIN);
            params2.put("record_id",record.getString("id"));
            params2.put("record_line_id",record.getString("line_id"));
            params2.put("login_token",DNSPOD_TOKEN_ID+","+DNSPOD_TOKEN);//接口需要tokenID+token组合发送
            params2.put("value",ip);
            String resJson = HttpUtil.post("https://dnsapi.cn/Record.Ddns", null, params2, "utf-8");

            //如果请求失败，打印失败信息
            JSONObject jsonObject = JSONObject.parseObject(resJson);
            JSONObject statusRes2 = JSONObject.parseObject(jsonObject.getString("status"));
            if(!statusRes2.getString("code").equals("1")){
                String msg=DateUtil.formatDateTime(date) +" error->"+statusRes2.getString("message")+"\n";
                fileWriter.write(msg);
                return;
            }

            fileWriter.write(DateUtil.formatDateTime(date) +" info->已修改为最新IP:"+ip+"\n");


        }catch (Exception e){
            String msg=DateUtil.formatDateTime(date) +" error->"+e.getMessage()+"\n";
            fileWriter.write(msg);
        }finally {
            fileWriter.close();
        }


    }
}
